/* * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.tramsun.libs.prefcompatsample.slice;

import com.tramsun.libs.prefcompatsample.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import com.tramsun.libs.prefcompat.Pref;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.LayoutScatter;
import ohos.agp.window.dialog.ToastDialog;
import ohos.app.Context;
import ohos.eventhandler.EventHandler;
import ohos.eventhandler.EventRunner;
import java.io.Serializable;
import java.util.Arrays;


public class MainAbilitySlice extends AbilitySlice {
    private static final String TAG = "MainAbilitySlice";
    public static final String SP_SCORES = "SP_SCORES";
    public static final String AGE = "AGE";
    public static final String USER1 = "USER1";
    public static final String PERSON1 = "PERSON1";
    private CompositeDisposable disposables;
    Button incrementButton;
    Context mcontext;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);

        mcontext = getApplicationContext();
        ComponentContainer rootLayout = (ComponentContainer) LayoutScatter.getInstance(this)
                .parse(ResourceTable.Layout_ability_main, null, false);

        disposables = new CompositeDisposable();

        // Named SharedPreferences
        Pref.from(SP_SCORES).putDoubleList(USER1, Arrays.asList(1.1, 2.2, 3.33));
        LogUtil.info(TAG,  "scores of USER1 =" + Pref.from(SP_SCORES).getDoubleList(USER1).toString());

        // Default SharedPreferences
        Pref.putObject(PERSON1, new Person("Tushar", 24));
        LogUtil.info(TAG,  "PERSON1=" + Pref.getObject(PERSON1, Person.class));


        Button incrementButton = (Button) rootLayout.findComponentById(ResourceTable.Id_increment);
        incrementButton.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {

                LogUtil.info(TAG, "Incrementing age from " + Pref.getInt(AGE, 0));
                Pref.putInt(AGE, Pref.getInt(AGE, 0) + 1);
            }
        });

        // Listen on a key for default SharedPreferences
        Disposable disposable1 = Pref.listenOn(AGE).subscribe(new Consumer<String>() {
            @Override public void accept(String key) throws Exception {
                LogUtil.info(TAG,  "1. Value of " + key + " changed to " + Pref.getInt(key));
                ToastDialog toast = new ToastDialog(mcontext);
                toast.setText("1. Value of " + key + " changed to " + Pref.getInt(key)).show();
            }
        });
        disposables.add(disposable1);

        // Listen on a key for named SharedPreferences
        Disposable disposable2 = Pref.from(SP_SCORES).listenOn(USER1).subscribe(new Consumer<String>() {
            @Override public void accept(String key) throws Exception {
                LogUtil.info(TAG,  "2. Scores of " + key + " changed to " + Pref.from(SP_SCORES)
                        .getDoubleList(key)
                        .toString());
            }
        });
        disposables.add(disposable2);

        new EventHandler(EventRunner.current()).postTask(new Runnable() {
            @Override public void run() {
                Pref.from(SP_SCORES).putDoubleList(USER1, Arrays.asList(3.3, 4.4, 5.5));
            }
        }, 1000);

        super.setUIContent(rootLayout);
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    @Override
    protected void onStop() {
        super.onStop();

        if (disposables != null) {
            disposables.dispose();
            disposables = null;
        }
    }

    static class Person implements Serializable {
        String name;
        int age;

        public Person(String name, int age) {
            this.name = name;
            this.age = age;
        }

        @Override public String toString() {
            return "Person{" + "name='" + name + '\'' + ", age=" + age + '}';
        }
    }

}
